//construit le menu non connect� � partir du JSON
function buildMenu(jsmenu) 
{
	var json=eval(" (" + jsmenu + ") ");
	var ulmenu = document.createElement('ul');
	ulmenu.id='nav';
	var element = document.getElementById("menu");
	element.appendChild(ulmenu);
	for(var i = 0; i < 4; i++)
	{
	var limenu = document.createElement('li');
	var amenu = document.createElement('a');
	amenu.href=json.action[i].href;
	texte = document.createTextNode(json.action[i].title);
	amenu.appendChild(texte);
	limenu.appendChild(amenu);
	ulmenu.appendChild(limenu);
		}
	document.getElementById("menu").innerHTML += "</ul>";
}
	
	
function ecrire_cookie(nom, valeur, expires) {
  
if (expires == null) {
var expires = new Date();  
  expires.setTime (expires.getTime() +  (1000*60*60)); 
}
   else {
   var expires = new Date(); 
   expires.setTime (expires.getTime() - (1000*60*60));
}
  document.cookie=nom+"="+escape(valeur)+
  ((expires==null) ? "" : ("; expires="+expires.toGMTString()));
}

function supprimer_cookie (nom) {
ecrire_cookie (nom, "", -1);
}


function lire_cookie(nom) {
  var arg=nom+"=";
  var alen=arg.length;
  var clen=document.cookie.length;
  var i=0;
  while (i<clen){
    var j=i+alen;
    if (document.cookie.substring(i, j)==arg) {
		//element.innerHTML = arguments_cookies(j);
       return ;
	   }
    i=document.cookie.indexOf(" ",i)+1;
    if (i==0) break;
  }
  return;
}

var file = './dynMenu.json';

//r�cup�re le JSON sur le serveur et lance le menu adapt�
function loadJSON(bool) 
{
	try {
	var http= new XMLHttpRequest();
	http.open("GET", file,true);
	http.onreadystatechange=function() 
	{
		if (http.readyState == 4) 
		{
			if (http.status != 404) 
			{
				if (bool) {
				buildConnectedMenu(http.responseText);
				}
				else {
				document.getElementById("menu").innerHTML = http.responseText ;
				buildMenu(http.responseText);
				}
			}				
			else 
			{
				document.getElementById("menu").innerHTML = file + " not found";
			}
		}
	}
	http.send(null);
}
catch (XMLerror) {
	};
}

//construit le menu connect�
function buildConnectedMenu (jsonfile) {
	var json=eval(" (" + jsonfile + ") ");
	var ulmenu = document.createElement('ul');
	ulmenu.id='nav';
	var element = document.getElementById("menu");
	element.appendChild(ulmenu);
		for (i=0;i<5;i++) {
		var limenu = document.createElement('li');
		var amenu = document.createElement('a');
		amenu.href=json.commands[i].href;
		texte = document.createTextNode(json.commands[i].title);
		amenu.appendChild(texte);
		limenu.appendChild(amenu);
		ulmenu.appendChild(limenu);
		}
		document.getElementById("menu").innerHTML += "</ul>";
	}